package com.hnu.java.urltest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public class Demo1 {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		/*final StringBuffer test = new StringBuffer("abc");
		test.append("d");
		System.out.println(test);*/
		
		/*String test = null;
		if(test.equals("abc"))
		{
			System.out.println(test);
		}*/
		
		/*String a ="abc";
		String a1 =a;
		String b = new String("abc");
		System.out.println(a==a1);
		System.out.println(a==b);
		System.out.println(a.equals(b));*/
		
		/*String str = "13abf";
		int len = str.length();
		int sum = 0;
		for(int i=0;i<len;i++){
			char c = str.charAt(len-1-i);
			int n = Character.digit(c,16);
			System.out.println(1<<4);
			
		}*/
//		String urlJson="http://localhost:8080/javaweb/servlettest";
//		apacheHttp(urlJson);
//		String urlHtml="http://localhost:8080/javaweb/index.jsp";
//		apacheHttp(urlHtml);
		javaUrl();
		
	}
	
	public static void javaUrl(){
		BufferedReader in = null;
		BufferedWriter out = null;
		File f  = new File("E:/TestFile/a.html");
		
		try {
			URL url = new URL("http://www.baidu.com");
			URLConnection urlConnection  = url.openConnection();
			HttpURLConnection httoConnection =(HttpURLConnection) urlConnection;
			System.out.println(httoConnection.getContentType());
			System.out.println(httoConnection.getContentEncoding());
			System.out.println(httoConnection.getResponseCode());
			if(httoConnection.getResponseCode()!=200){
				System.out.println("服务器响应失败！");
				return;
			}
		    in = new BufferedReader(new InputStreamReader(url.openStream(),"gb2312"));

		    out = new BufferedWriter(new FileWriter(f));
			String nextLine;
			while((nextLine=in.readLine())!=null)
			{
				out.write(nextLine);
			}
			out.flush();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("无效的url地址！");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("读取资源出现错误");
		}finally{
			if(in!=null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("xxoo");
				}
				if(out!=null)
					try {
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("xxoo");
					}
		}
	}
	
	public static void apacheHttp(String url) throws ClientProtocolException, IOException{
		HttpClient hc = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse = hc.execute(httpGet);
		HttpEntity httpEntity = httpResponse.getEntity();
		String data = EntityUtils.toString(httpEntity);
		System.out.println(data);
	}
	
	public  void test(int a, int b ,int c)
	{
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
	}

}
